{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "     花萼长度  花萼宽度  花瓣长度  花瓣宽度  种类\n0     5.1   3.5   1.4   0.2   0\n1     4.9   3.0   1.4   0.2   0\n2     4.7   3.2   1.3   0.2   0\n3     4.6   3.1   1.5   0.2   0\n4     5.0   3.6   1.4   0.2   0\n..    ...   ...   ...   ...  ..\n145   6.7   3.0   5.2   2.3   2\n146   6.3   2.5   5.0   1.9   2\n147   6.5   3.0   5.2   2.0   2\n148   6.2   3.4   5.4   2.3   2\n149   5.9   3.0   5.1   1.8   2\n\n[150 rows x 5 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>花萼长度</th>\n      <th>花萼宽度</th>\n      <th>花瓣长度</th>\n      <th>花瓣宽度</th>\n      <th>种类</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>5.1</td>\n      <td>3.5</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4.9</td>\n      <td>3.0</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4.7</td>\n      <td>3.2</td>\n      <td>1.3</td>\n      <td>0.2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4.6</td>\n      <td>3.1</td>\n      <td>1.5</td>\n      <td>0.2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5.0</td>\n      <td>3.6</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>145</th>\n      <td>6.7</td>\n      <td>3.0</td>\n      <td>5.2</td>\n      <td>2.3</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>146</th>\n      <td>6.3</td>\n      <td>2.5</td>\n      <td>5.0</td>\n      <td>1.9</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>147</th>\n      <td>6.5</td>\n      <td>3.0</td>\n      <td>5.2</td>\n      <td>2.0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>148</th>\n      <td>6.2</td>\n      <td>3.4</td>\n      <td>5.4</td>\n      <td>2.3</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>149</th>\n      <td>5.9</td>\n      <td>3.0</td>\n      <td>5.1</td>\n      <td>1.8</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n<p>150 rows × 5 columns</p>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = pd.read_excel('花.xlsx')\n",
    "iris"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X = iris[['花萼长度', '花萼宽度', '花瓣长度', '花瓣宽度']]\n",
    "Y = iris['种类']\n",
    "X_train, X_test, Y_train, Y_test = train_test_split(\n",
    "    X, Y, test_size=0.3\n",
    ")  # 第一个参数：特征   第二个参数：标签   test_size：训练集与测试集的比值"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "KNeighborsClassifier()"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier  # K近邻\n",
    "\n",
    "knn = KNeighborsClassifier()\n",
    "knn.fit(X_train, Y_train)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "     真实值  预测值\n99     1    1\n41     0    0\n29     0    0\n26     0    0\n7      0    0\n147    2    2\n54     1    1\n62     1    1\n95     1    1\n24     0    0\n98     1    1\n17     0    0\n52     1    1\n85     1    1\n87     1    1\n22     0    0\n13     0    0\n33     0    0\n50     1    1\n64     1    1\n72     1    2\n39     0    0\n10     0    0\n8      0    0\n37     0    0\n59     1    1\n77     1    2\n94     1    1\n114    2    2\n89     1    1\n65     1    1\n43     0    0\n75     1    1\n49     0    0\n32     0    0\n78     1    1\n23     0    0\n2      0    0\n91     1    1\n111    2    2\n28     0    0\n84     1    1\n51     1    1\n21     0    0\n6      0    0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>真实值</th>\n      <th>预测值</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>99</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>41</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>29</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>26</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>147</th>\n      <td>2</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>54</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>62</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>95</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>24</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>98</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>17</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>52</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>85</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>87</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>22</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>33</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>50</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>64</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>72</th>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>39</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>37</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>59</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>77</th>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>94</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>114</th>\n      <td>2</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>89</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>65</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>43</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>75</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>49</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>32</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>78</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>23</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>91</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>111</th>\n      <td>2</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>28</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>84</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>51</th>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>21</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.DataFrame()\n",
    "data['真实值'] = Y_test\n",
    "data['预测值'] = knn.predict(X_test)\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00        21\n",
      "           1       1.00      0.90      0.95        21\n",
      "           2       0.60      1.00      0.75         3\n",
      "\n",
      "    accuracy                           0.96        45\n",
      "   macro avg       0.87      0.97      0.90        45\n",
      "weighted avg       0.97      0.96      0.96        45\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "\n",
    "print(classification_report(data['真实值'],data['预测值']))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}